package cn.liuweiwei.controller;

import cn.liuweiwei.domain.Blog;
import cn.liuweiwei.service.BlogService;
import cn.liuweiwei.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("blogs")
@CrossOrigin
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 通过博客标题模糊查询博客
     *
     * @param page  当前页
     * @param limit 每页条数
     * @param title 博客标题
     * @return 查询到的博客列表，不包含博客详情
     */
    @GetMapping
    public Result findBlogsByTitle(String page, String limit,
                                   @RequestParam(defaultValue = "", required = false) String title) {
        PageInfo<Blog> pageInfo = blogService.findBlogsByTitle(page, limit, title);
        return Result.successWithCodeAndObj(pageInfo);
    }

    /**
     * 分页查询博客
     *
     * @param page  当前页
     * @param limit 每页条数
     * @return 查询到的博客列表，不包含博客详情
     */
    @GetMapping("page/{page}/{limit}")
    public Result findBlogsNoContent(@PathVariable("page") String page,
                                     @PathVariable("limit") String limit) {
        PageInfo<Blog> blogPageInfo = blogService.findBlogsNoContent(page, limit);
        return Result.successWithObj(blogPageInfo);
    }

    /**
     * 分页查询最新的博客
     *
     * @return 返回最新的博客列表，不包含博客详情
     */
    @GetMapping("time/{page}/{limit}")
    public Result orderBlogByTime(@PathVariable("page") String page,
                                  @PathVariable("limit") String limit) {
        return Result.successWithObj(blogService.orderBlogsByTime(page, limit));
    }

    /**
     * 根据博客 id 查询博客，包含博客详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findOneBlogById(@PathVariable("id") String id) {
        Blog blog = blogService.findOneBlogById(id);
        return Result.successWithObj(blog);
    }

    /**
     * 插入一条博客
     *
     * @param blog
     * @return
     */
    @PostMapping
    public Result insertOneBlog(@RequestBody Blog blog) {
        try {
            blogService.insertOneBlog(blog);
            return Result.success("添加博客成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加博客失败");
        }
    }

    /**
     * 更新博客
     *
     * @param blog
     * @return
     */
    @PutMapping
    public Result updateBlog(@RequestBody Blog blog) {
        try {
            blogService.updateBlog(blog);
            return Result.success("博客更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("博客更新失败");
        }
    }

    /**
     * 根据博客 id 删除博客
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result deleteOneBlog(@PathVariable("id") String id) {
        try {
            blogService.deleteOneBlog(id);
            return Result.success("删除博客成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除博客失败");
        }
    }

}
